package where2go.sctu.com.zhiyou.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import where2go.sctu.com.zhiyou.MyApplication;
import where2go.sctu.com.zhiyou.R;
import where2go.sctu.com.zhiyou.activity.HelpFeedbackActivity;
import where2go.sctu.com.zhiyou.activity.LoginActivity;
import where2go.sctu.com.zhiyou.activity.MyCollectionActivity;
import where2go.sctu.com.zhiyou.activity.MyInformationActivity;
import where2go.sctu.com.zhiyou.activity.MyOdringActivity;
import where2go.sctu.com.zhiyou.activity.MyPurchareActivity;
import where2go.sctu.com.zhiyou.activity.MySetingActivity;
import where2go.sctu.com.zhiyou.activity.MyShoppingActivity;
import where2go.sctu.com.zhiyou.activity.MyStudyActivity;
import where2go.sctu.com.zhiyou.activity.MyWalletActivity;
import where2go.sctu.com.zhiyou.activity.PersonalImformationActivity;
import where2go.sctu.com.zhiyou.entity.NewUser;
import where2go.sctu.com.zhiyou.utils.HeaderStringRequest;
import where2go.sctu.com.zhiyou.utils.ShareUtils;

/**
 * ProjectName: zhiyou
 * Created by 张某人
 * CreatedTime:on 2018/4/25   23:02
 * Description: 我的fragment
 */
public class OwnerFragment extends Fragment implements View.OnClickListener {


    private CircleImageView avatar;
    private TextView username;

    private NewUser.DataBean user = new NewUser.DataBean();

    private NewUser users = new NewUser();
    private String url1 = "http://47.104.80.155/youyiyou/resources/img/";

    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;
    private LinearLayout linearLayout5;
    private LinearLayout linearLayout6;
    private LinearLayout linearLayout7;
    private LinearLayout linearLayout8;
    private LinearLayout linearLayout9;
    private LinearLayout linearLayout0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment, container, false);

        findView(view);
        return view;

    }


    private void findView(View view) {

        avatar = (CircleImageView) view.findViewById(R.id.imageView_userIcon);
        username = (TextView) view.findViewById(R.id.txt_username);
        avatar.setOnClickListener(this);
        username.setOnClickListener(this);

        linearLayout1 = view.findViewById(R.id.persnal_information3);
        linearLayout2 = view.findViewById(R.id.in_center);
        linearLayout3 = view.findViewById(R.id.my_collection3);
        linearLayout4 = view.findViewById(R.id.my_shopping);
        linearLayout5 = view.findViewById(R.id.my_oder);
        linearLayout6 = view.findViewById(R.id.my_purchase);
        linearLayout7 = view.findViewById(R.id.my_wallets);
        linearLayout8 = view.findViewById(R.id.my_set);
        linearLayout9 = view.findViewById(R.id.help_feedbacks);
        linearLayout0 = view.findViewById(R.id.my_studyly);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);
        linearLayout5.setOnClickListener(this);
        linearLayout6.setOnClickListener(this);
        linearLayout7.setOnClickListener(this);
        linearLayout8.setOnClickListener(this);
        linearLayout9.setOnClickListener(this);
        linearLayout0.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.persnal_information3:
                if (ShareUtils.getString(getContext(), "token", null) == null) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), PersonalImformationActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.in_center:
                Intent intent1 = new Intent();
                intent1.setClass(getActivity(), MyInformationActivity.class);
                startActivity(intent1);
                break;
            case R.id.my_collection3:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MyCollectionActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.my_shopping:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MyShoppingActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.my_oder:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MyOdringActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.my_purchase:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MyPurchareActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.my_wallets:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MyWalletActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.my_set:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), MySetingActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            case R.id.help_feedbacks:
                Intent intent8 = new Intent();
                intent8.setClass(getActivity(), HelpFeedbackActivity.class);
                startActivity(intent8);
                break;
            case R.id.my_studyly:
                if (ShareUtils.getString(getContext(), "token", null) == null) {
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), MyStudyActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.imageView_userIcon:
            case R.id.txt_username:
                if (ShareUtils.getString(getContext(), "token", null) != null){
                    startActivity(new Intent(getActivity(), PersonalImformationActivity.class));
                }else{
                    Toast.makeText(getContext(),"请先登录",Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
                break;
            default:
                break;
        }
    }

    private void getAvatarAndUsername() {
        String url = "http://47.104.80.155/youyiyou/users/details/show";
        HeaderStringRequest request = new HeaderStringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                Type type = new TypeToken<NewUser>() {
                }.getType();
                users = gson.fromJson(response, type);
                user = users.getData();
                username.setText(user.getUserName());

                ImageRequest imageRequest = new ImageRequest(url1 + user.getUserAvatarAddress(),
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap bitmap) {
                                avatar.setImageBitmap(bitmap);
                            }
                        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //请求失败 设置默认图片
                        avatar.setImageResource(R.drawable.avatar);
                    }

                });
                MyApplication.getHttpQueues().add(imageRequest);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("authorization", ShareUtils.getString(getActivity(), "token", null));
                return map;
            }
        };
        MyApplication.getHttpQueues().add(request);
    }
}

